# Copyright 2023 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:enforce_glob.bzl", "enforce_glob")
load("//build_tools/bazel:iree_check_test.bzl", "iree_check_single_backend_test_suite")

package(
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

ALL_SRCS = enforce_glob(
    [
        "abs.mlir",
        "add.mlir",
        "batch_norm_inference.mlir",
        "bitcast_convert.mlir",
        "broadcast.mlir",
        "broadcast_add.mlir",
        "broadcast_in_dim.mlir",
        "clamp.mlir",
        "compare.mlir",
        "complex.mlir",
        "concatenate.mlir",
        "constant.mlir",
        "convert.mlir",
        "convolution.mlir",
        "cosine.mlir",
        "divide.mlir",
        "dot.mlir",
        "dot_bf16.mlir",
        "dot_general.mlir",
        "dynamic_slice.mlir",
        "dynamic_update_slice.mlir",
        "exponential.mlir",
        "exponential_fp16.mlir",
        "exponential_minus_one.mlir",
        "fft.mlir",
        "finite.mlir",
        "floor.mlir",
        "gather.mlir",
        "householder.mlir",
        "iota.mlir",
        "log.mlir",
        "log_plus_one.mlir",
        "maximum.mlir",
        "minimum.mlir",
        "multiply.mlir",
        "negate.mlir",
        "pad.mlir",
        "philox.mlir",
        "pow.mlir",
        "reduce.mlir",
        "reduce_window.mlir",
        "remainder.mlir",
        "reshape.mlir",
        "reverse.mlir",
        "rng_normal.mlir",
        "rng_uniform.mlir",
        "round.mlir",
        "rsqrt.mlir",
        "scatter.mlir",
        "scatter_dynamic.mlir",
        "select.mlir",
        "shape_assertion.mlir",
        "sine.mlir",
        "slice.mlir",
        "sort.mlir",
        "sqrt.mlir",
        "subtract.mlir",
        "tanh.mlir",
        "three_fry.mlir",
        "torch_index_select.mlir",
        "transpose.mlir",
        "while.mlir",
    ],
    include = ["*.mlir"],
    exclude = [],
)

iree_check_single_backend_test_suite(
    name = "check_llvm-cpu_local-task",
    srcs = ALL_SRCS,
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
        "--iree-llvmcpu-target-cpu=generic",
    ],
    driver = "local-task",
    tags = [
        # round_nearest_afz/fmodf fail with a wasm target, just disable all tests there for now
        #   undefined symbol: round_nearest_afz/fmodf
        "nowasm",
    ],
    target_backend = "llvm-cpu",
)

# Check host features compilation (LLVM backend with host cpu features).
iree_check_single_backend_test_suite(
    name = "check_llvm-cpu-host_local-task",
    srcs = ALL_SRCS,
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
        "--iree-llvmcpu-target-cpu=host",
    ],
    driver = "local-task",
    # Building and testing must be on the same architecture, which doesn't work
    # with remote execution in general.
    tags = [
        "hostonly",
        "local",
    ],
    target_backend = "llvm-cpu",
)

iree_check_single_backend_test_suite(
    name = "check_vmvx_local-task",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "bitcast_convert.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "complex.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "fft.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "householder.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "philox.mlir",
            "pow.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rng_normal.mlir",
            "rng_uniform.mlir",
            "round.mlir",
            "rsqrt.mlir",
            "scatter.mlir",
            "scatter_dynamic.mlir",
            "select.mlir",
            "shape_assertion.mlir",
            "sine.mlir",
            "slice.mlir",
            "sort.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "three_fry.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "dot_bf16.mlir",
            "exponential_fp16.mlir",
        ],
    ),
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
    ],
    driver = "local-task",
    target_backend = "vmvx",
)

iree_check_single_backend_test_suite(
    name = "check_vulkan-spirv_vulkan",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "bitcast_convert.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "complex.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_bf16.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "householder.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "philox.mlir",
            "pow.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "rng_normal.mlir",
            "rng_uniform.mlir",
            "round.mlir",
            "rsqrt.mlir",
            "scatter.mlir",
            "scatter_dynamic.mlir",
            "select.mlir",
            "shape_assertion.mlir",
            "sine.mlir",
            "slice.mlir",
            "sort.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "three_fry.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "exponential_fp16.mlir",
            "fft.mlir",  # TODO(#9583)
            "reverse.mlir",  # TODO(#12415): disabled due to miscompilation on Pixel 6.
        ],
    ),
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
    ],
    driver = "vulkan",
    target_backend = "vulkan-spirv",
)

iree_check_single_backend_test_suite(
    name = "check_cuda_graph",
    srcs = ALL_SRCS,
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
        # TODO(#13984): memset emulation required for graphs.
        "--iree-stream-emulate-memset",
    ],
    driver = "cuda",
    runner_args = ["--cuda_use_streams=false"],
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_cuda_stream",
    srcs = ALL_SRCS,
    compiler_flags = [
        "--iree-input-demote-f64-to-f32",
    ],
    driver = "cuda",
    runner_args = ["--cuda_use_streams=true"],
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_rocm_hip_stream",
    srcs = ALL_SRCS,
    driver = "hip",
    runner_args = [
        "--hip_use_streams=true",
    ],
    target_backend = "rocm",
)

iree_check_single_backend_test_suite(
    name = "check_metal-spirv_metal",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "bitcast_convert.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "complex.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_bf16.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_fp16.mlir",
            "exponential_minus_one.mlir",
            "fft.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "householder.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "philox.mlir",
            "pow.mlir",
            "reduce.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rng_normal.mlir",
            "rng_uniform.mlir",
            "round.mlir",
            "rsqrt.mlir",
            "scatter.mlir",
            "scatter_dynamic.mlir",
            "select.mlir",
            "shape_assertion.mlir",
            "sine.mlir",
            "slice.mlir",
            "sort.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "three_fry.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "reduce_window.mlir",  # TODO(#15012): fix test crash
        ],
    ),
    driver = "metal",
    target_backend = "metal-spirv",
)
